package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.rules.GoodsSelectAttributeAdapter;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/8/25.
 *         <p/>
 *         选择属性
 */

public class GoodsAttributesSelectFragment extends TheParentFragment {

    public RecyclerView mRvContent;
    public GoodsSelectAttributeAdapter mAdapter;
    public List<GoodsAttributesBean.ListBean> tagBeanList = new ArrayList<>();
    public static final int SELECT_ATTRIBUTE_BYNAME = 1;  //根据属性名模糊查找属性
    private String searchName;
    private List<GoodsAttributesBean.ListBean> selectedList;

    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_select_application_objectst;
    }
    @Override
    public void setTitle() {
        if (peekFragment() instanceof GoodsAttributesSelectFragment){
            mActivity.setTitle(R.string.attributes_select);
        }
    }
    @Override
    public void onSuccess(CommonClass commonClass, int i) {
        switch (i) {
            case SELECT_ATTRIBUTE_BYNAME:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    GoodsAttributesBean goodsAttributesBean = JSON.parseObject(commonClass.getData().toString(), GoodsAttributesBean.class);
                    ArrayList<GoodsAttributesBean.ListBean> list = (ArrayList<GoodsAttributesBean.ListBean>) goodsAttributesBean.getList();
                    tagBeanList.addAll(list);
                    if (null!=selectedList){
                        for (int i1 = 0; i1 < tagBeanList.size(); i1++) {
                            for (int i2 = 0; i2 < selectedList.size(); i2++) {
                                if (tagBeanList.get(i1).getAttributeUkid()==selectedList.get(i2).getAttributeUkid()){
                                    tagBeanList.get(i1).setSelect(true);
                                    tagBeanList.get(i1).setFromCategory(selectedList.get(i2).isFromCategory()?true:false);
                                }
                            }
                        }
                    }
                    final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            observer.removeGlobalOnLayoutListener(this);
                            setHeight(mRvContent.getHeight(), mRvContent.getWidth());
                        }
                    });
                }
                break;
        }

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 3) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public boolean canScrollVertically() {
                return false;
            }

        });
    }

    public void setHeight(int height, int Width) {
        LogUtils.showInfoLog("height" + height + "Width" + Width);
        mAdapter = new GoodsSelectAttributeAdapter(mActivity, tagBeanList, height, Width);
        mRvContent.setAdapter(mAdapter);
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        selectedList = b.getParcelableArrayList("selectedAttribute");
        searchName = b.getString("attributeName");
        int pos = b.getInt("pos");//当前几页
        int num = b.getInt("num");//总共几页
         GetData(pos,searchName);
    }

    public void GetData(int pos, String searchName) {
        Map<String, Object> map = new HashMap();
        Map<String, Object> query = new HashMap<>();
        if (TextUtils.isEmpty(searchName)){
            searchName = "";
        }
        map.put("attributeName", searchName);
        query.put("page", pos);
        query.put("size", 24);
        query.put("sort", "create_time asc");
        map.put("query", query);
        httpPost(ResourceConstant.SELECT_ATTRIBUTE_BYNAME, map, SELECT_ATTRIBUTE_BYNAME, false, "");
    }
}
